﻿using GalaSoft.MvvmLight.Messaging;
using NLog.Config;
using OneOfAll.Common.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneOfAll.Common.Models
{
    public class GlobalResourceModel : BaseModel
    {
        #region 路径
        public string ArchivesDir = $"{System.AppDomain.CurrentDomain.BaseDirectory}Archives";
        public string ConstructionResourcePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}ResourcesConfig\\ConstructionResourceConfig.xml";
        #endregion

        #region 随机人物
        public List<string> LastNameList = new List<string>() { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "长孙", "司马", "诸葛", "南宫", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "张", "施", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "谢", "邹", "喻", "柏", "水", "章", "云", "苏", "潘", "范", "彭", "郎", "鲁", "韦", "马", "方", "任", "柳", "史", "唐", "雷", "贺", "罗", "毕", "乐", "于", "元", "黄" };
        public List<string> ManFirstNameList = new List<string>() { "建国", "铁柱", "伟", "浩", "正南", "浩然", "子建", "飞宇", "彦宏", "承东", "正飞", "军", "明", "作虎", "程", "思聪", "建林", "嘉城", "典", "言冰", "肖", "锋", "本伟", "涛", "坤", "墩宇", "锐", "玉箫", "力豪", "默", "狗蛋", "二狗", "复兴", "风", "子轩", "敦夏", "负浪", "信品", "仁毅", "州龙", "单羽", "行童", "凉翼", "栋昆", "修全", "江冠", "斌", "冉伯", "志承", "民佑", "冬旭", "云翔", "方舟", "材宁", "言邦", "博贤", "树旗", "尉寒", "绍" };
        public List<string> WomanFirstNameList = new List<string>() { "婉儿", "思雨", "思甜", "嫣然", "子鱼", "柔", "静雯", "语嫣", "梦琪", "忆柳", "之桃", "幕青", "元香", "初夏", "曼文", "代云" };
        public List<string> AddrList = new List<string>() { "冀州", "兖州", "青州", "徐州", "扬州", "荆州", "豫州", "梁州", "雍州" };
        public List<string> SpecialList = new List<string>() { "安分守己", "严于律己", "表里如一", "雷厉风行", "憨厚老实", "沉着稳重", "善良淳朴", "一丝不苟", "勤奋刻苦", "意志坚强", "不畏权势", "刚正不阿", "正直无私", "有勇有谋", "德才兼备", "洁身自好", "天真活泼", "重情重义", "成熟稳重", "小心谨慎", "宽宏大量", "待人诚恳" };
        #endregion

        #region 消息
        public string CameraMove_Battle = "CameraMove_Battle";
        public string CameraMove_City = "CameraMove_City";
        public string MouseMove_City = "MouseMove_City";
        public string ArchiveChanged = "ArchiveChanged";
        public string CityWalkParamsChanged = "CityWalkParamsChanged";
        public string CloseChildrenWindow = "CloseChildrenWindow";
        public string ShowChildrenInfo = "ShowChildrenInfo";
        public string ShowPeopleInfo = "ShowPeopleInfo";
        #endregion

        #region 字段
        public int GridSize = 10;
        public string archiveExtension = ".ooa";
        #endregion

        #region 状态
        public bool IsRunning = true;
        #endregion

        #region 参数
        private int gameSpeed = 250;
        public int GameSpeed
        {
            get { return gameSpeed; }
            set { gameSpeed = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 枚举资源
        private ObservableCollection<ConstructionTypeEnum> constructionTypeList = new ObservableCollection<ConstructionTypeEnum>();
        public ObservableCollection<ConstructionTypeEnum> ConstructionTypeList
        {
            get { return constructionTypeList; }
            set { constructionTypeList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<MaterialsTypeEnum> materialsTypeList = new ObservableCollection<MaterialsTypeEnum>();
        public ObservableCollection<MaterialsTypeEnum> MaterialsTypeList
        {
            get { return materialsTypeList; }
            set { materialsTypeList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<ProductTypeEnum> productTypeList = new ObservableCollection<ProductTypeEnum>();
        public ObservableCollection<ProductTypeEnum> ProductTypeList
        {
            get { return productTypeList; }
            set { productTypeList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<ResourceTypeEnum> resourceTypeList = new ObservableCollection<ResourceTypeEnum>();
        public ObservableCollection<ResourceTypeEnum> ResourceTypeList
        {
            get { return resourceTypeList; }
            set { resourceTypeList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 全局资源
        private ResourceConfigModel resourceConfig = null;
        public ResourceConfigModel ResourceConfig
        {
            get { return resourceConfig; }
            set { resourceConfig = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<ArchiveModel> archiveList = new ObservableCollection<ArchiveModel>();
        public ObservableCollection<ArchiveModel> ArchiveList
        {
            get { return archiveList; }
            set { archiveList = value; RaisePropertyChanged(); }
        }

        private ArchiveModel archiveInfo = null;
        public ArchiveModel ArchiveInfo
        {
            get { return archiveInfo; }
            set { archiveInfo = value; RaisePropertyChanged(); Messenger.Default.Send(value, ArchiveChanged); }
        }
        #endregion





        public GlobalResourceModel()
        {
            InitGlobalList();
            LoadConfig();
            LoadResource();
            RefushArchives();
        }

        private void InitGlobalList()
        {
            Enum.GetNames(typeof(ConstructionTypeEnum)).ForEach(t => ConstructionTypeList.Add((ConstructionTypeEnum)Enum.Parse(typeof(ConstructionTypeEnum), t)));
            Enum.GetNames(typeof(MaterialsTypeEnum)).ForEach(t => MaterialsTypeList.Add((MaterialsTypeEnum)Enum.Parse(typeof(MaterialsTypeEnum), t)));
            Enum.GetNames(typeof(ProductTypeEnum)).ForEach(t => ProductTypeList.Add((ProductTypeEnum)Enum.Parse(typeof(ProductTypeEnum), t)));
            Enum.GetNames(typeof(ResourceTypeEnum)).ForEach(t => ResourceTypeList.Add((ResourceTypeEnum)Enum.Parse(typeof(ResourceTypeEnum), t)));
        }

        private void LoadConfig()
        {
        }

        #region 读取资源配置
        private void LoadResource()
        {
            try
            {
                ResourceConfig = new ResourceConfigModel();
                LoadConstructionResource();
            }
            catch (Exception ex)
            {
                LogHelper.Instance.GetLog(LogTypeEnum.Error, "资源配置加载失败", ex);
            }
        }

        private void LoadConstructionResource()
        {
            Directory.CreateDirectory(Path.GetDirectoryName(ConstructionResourcePath)); ;
            if (File.Exists(ConstructionResourcePath))
                ResourceConfig.ConstructionList = (ObservableCollection<ConstructionModel>)XmlHelper.ReadXml(typeof(ObservableCollection<ConstructionModel>), ConstructionResourcePath);
            else
                CreateConstructionResource();
        }

        private void CreateConstructionResource()
        {
            ResourceConfig.ConstructionList = new ObservableCollection<ConstructionModel>();
            SaveConstructionResource();
        }

        public void SaveConstructionResource()
        {
            try
            {
                Directory.CreateDirectory (Path.GetDirectoryName(ConstructionResourcePath));
                XmlHelper.SaveXml(new ObservableCollection<ConstructionModel>( ResourceConfig.ConstructionList.OrderBy(t => t.Quality).ToList()), ConstructionResourcePath);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.GetLog(LogTypeEnum.Error, "保存建筑资源", ex);
            }
        }
        #endregion

        private void RefushArchives()
        {
            ArchiveList.Clear();
            if (!Directory.Exists(ArchivesDir))
                Directory.CreateDirectory(ArchivesDir);
            LogHelper.Instance.GetLog(LogTypeEnum.Debug, $"RefushArchives{ArchivesDir}");
            foreach (var file in Directory.GetFiles(ArchivesDir, $"*{archiveExtension}"))
            {
                try
                {
                    var archive = (ArchiveModel)XmlHelper.ReadXml(typeof(ArchiveModel), file);
                    if (archive != null)
                        ArchiveList.Add(archive);
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.GetLog(LogTypeEnum.Error, "解析存档", ex);
                }
            }

            
        }

        public void SaveArchive()
        {
            if (!Directory.Exists(ArchivesDir))
                Directory.CreateDirectory(ArchivesDir);
            XmlHelper.SaveXml(ArchiveInfo, $"{ArchivesDir}\\{ArchiveInfo.Key}{archiveExtension}");
        }
    }
}
